package com.itheima.bos.web.action.take_delivery;
/**
 * 宣传活动的action
 */
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.take_delivery.Promotion;
import com.itheima.bos.service.take_delivery.IPromotionService;
import com.itheima.bos.web.action.base.BaseAction;

@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class PromotionAction extends BaseAction<Promotion>{
	/*
	 * 上传保存宣传的活动
	 */
	@Autowired
	private IPromotionService promotionAerviceImpl;
	
	private File titleImgFile;
	private String titleImgFileFileName;
	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}
	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}




	@Action(value="promotion_Form",results={@Result(name="success",type="redirect",location="./pages/take_delivery/promotion.html")})
	public String promotion_Form() throws IOException{
		//得到保存文件的路径
		String savePath=ServletActionContext.getServletContext().getRealPath("/upload/");
		//保存图片的路径
		String saveUrl=ServletActionContext.getRequest().getContextPath()+"/upload/";
		//生成唯一的图片名保存
		UUID randomUUID = UUID.randomUUID();
		String ext = titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
		String randomName=randomUUID+ext;
		//保存哪里的什么文件图片
		FileUtils.copyFile(titleImgFile, new File(savePath+"/"+randomName));
		
		//将访问该图片的问的相对路径保存
		model.setTitleImg(saveUrl+randomName);
		
		promotionAerviceImpl.save(model);
		return "success";
		
	}
	
	@Action(value="findAllPromotion",results={@Result(name="success",type="json")})
	public String findAllPromotion(){
		//创建分页对象
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Promotion> pagedata= promotionAerviceImpl.findAll(pageable);
		getPageValueStack(pagedata);
		return SUCCESS;
	}
	
	
	
}
